#include "stdafx.h"
#include "bmp_rw.h"


#include <crtdbg.h>


bool BMP_RW::writeBmp(const char* name)
{
	

	BMFileHdr.bfType=0x4d42; 
	BMFileHdr.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER )+width*height*3; 
	BMFileHdr.bfReserved1=0; 
	BMFileHdr.bfReserved2=0; 
	BMFileHdr.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER ); 

	BMInfoHdr.biSize=sizeof(BITMAPINFOHEADER); 
	BMInfoHdr.biWidth=width; 
	BMInfoHdr.biHeight=height; 
	BMInfoHdr.biPlanes=1; 
	BMInfoHdr.biBitCount=24;
	BMInfoHdr.biCompression=BI_RGB; 
	BMInfoHdr.biSizeImage=width*height*3; 
	BMInfoHdr.biXPelsPerMeter=3780; 
	BMInfoHdr.biYPelsPerMeter=3780; 
	BMInfoHdr.biClrUsed=0; 
	BMInfoHdr.biClrImportant=0; 

	DWORD bytes_read;
   {
	HANDLE fh=CreateFile(

		//(LPCTSTR) &
		name,	// pointer to name of the file 
		GENERIC_WRITE,	// access (read-write) mode 
		FILE_SHARE_WRITE,	// share mode 
		0,	// pointer to security attributes 
		CREATE_ALWAYS,	// how to create 
		FILE_ATTRIBUTE_ARCHIVE	,	// file attributes 
		0	// handle to file with attributes to copy  
		);

//handle error
		if (fh==INVALID_HANDLE_VALUE)
			{
				_ASSERTE(false);
				return false;
			}

		WriteFile(
							fh,	// handle of file to read 
							(void*) &BMFileHdr,	// address of buffer that receives data  
							sizeof(BITMAPFILEHEADER),	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);
		WriteFile(
							fh,	// handle of file to read 
							(void*) &BMInfoHdr,	// address of buffer that receives data  
							sizeof(BITMAPINFOHEADER ),	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);
		WriteFile(
							fh,	// handle of file to read 
							(void*) BmpMap,	// address of buffer that receives data  
							width*height*3,	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);
		CloseHandle(fh);
	}

   return true;
}

bool BMP_RW::readBmp(const char* name)
{
	DWORD bytes_read;
	{
	HANDLE fh=CreateFile(name,
					   GENERIC_READ,
					   FILE_SHARE_READ	| FILE_SHARE_WRITE,
					   0,
					   OPEN_EXISTING,
					   0,
					   0
					   );

			if (fh==INVALID_HANDLE_VALUE)
			{
				_ASSERTE(false);
				return false;
			}

			DWORD dwHigh;
			DWORD fsize=::GetFileSize(fh,&dwHigh);

			_ASSERTE(fsize!=-1);
/*
			ReadFile( fh,
					(void*)stateBuf,
					fsize,
					&dwRead,
					0 );
*/
			ReadFile(
							fh,	// handle of file to read 
							(void*) &BMFileHdr,	// address of buffer that receives data  
							sizeof(BITMAPFILEHEADER),	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);
		ReadFile(
							fh,	// handle of file to read 
							(void*) &BMInfoHdr,	// address of buffer that receives data  
							sizeof(BITMAPINFOHEADER ),	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);


		width=BMInfoHdr.biWidth;
		height=BMInfoHdr.biHeight; 

		BmpMap=new BYTE[width*height*3];


		ReadFile(
							fh,	// handle of file to read 
							(void*) BmpMap,	// address of buffer that receives data  
							width*height*3,	// number of bytes to read 
							&bytes_read,	// address of number of bytes read 
							0// address of structure for data 
				);


			CloseHandle( fh );
	}
	return true;
};

void BMP_RW::freeMem()
{
    if (BmpMap) delete [] BmpMap;
};